//	ID : pallab81
// 	PROG : 11709
// 	LANG : C++
// 	2011-07-07-23.49.23 Thursday
//
// 	"I have not failed, I have just found 10000 ways that won't work.
//


#include <iostream>
#include <algorithm>
#include <vector>
#include <sstream>
#include <fstream>
#include <string>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <functional>
#include <bitset>

#include <cstdio>
#include <cmath>
#include <cstring>
#include <climits>
#include <cstring>

using namespace std;

#define fo(i,ed) for(int i = 0 ; i < ed ; ++i )
#define foit(i, x) for (typeof((x).begin()) i = (x).begin(); i != (x).end(); i++)
#define bip system("pause")
#define Int long long
#define pb push_back
#define SZ(X) (int)(X).size()
#define LN(X) (int)(X).length()
#define mk make_pair
#define f first
#define s second

int P,T;
map< string , int > ID;
vector< vector<int> > path, _path;
int seen1[ (int)1e3+50 ],seen2[ (int)1e3+50 ] ;
char buff1[400];
char buff2[400];
inline void eatIt(){
    gets(buff1);
}
inline void read() {
    eatIt();
    fo(i,P) {
        gets(buff1);
        ID[ (string)buff1 ] = i+1;
    }
    path.assign(P+5, vector<int>() );
    _path.assign(P+5, vector<int>() );
    fo(i,T) {
        gets(buff1);
        gets(buff2);
        int from = ID[ (string)buff1 ];
        int to   = ID[ (string)buff2 ];
        path[from].pb(to);
        _path[to].pb(from);
    }
}
inline void dfs(int now_node, stack<int> &nodes){
    if( seen1[now_node]==-1){
        seen1[now_node]=0;
        fo(i,SZ(path[now_node])){
            dfs( path[now_node][i], nodes);
        }
        nodes.push( now_node );
    }
}
inline void dfs2(int now_node){
    if( seen2[now_node]==-1){
        seen2[now_node]=0;
        fo(i,SZ(_path[now_node])){
            dfs2(_path[now_node][i]);
        }
    }
}
inline void proc(){
    memset(seen1,-1,sizeof(seen1));
    stack< int > nodes;
    for(int i=1; i<=P; ++i ){
        if(seen1[i]==-1){
            dfs(i,nodes);
        }
    }
    int tot=0;
    memset(seen2,-1,sizeof(seen2));
    for(int i ; nodes.empty()==false ; nodes.pop() ){
        i = nodes.top();
        if(seen2[i]==-1){
            dfs2(i);
            ++tot;
        }
    }
    printf("%d\n",tot);
}
int main() {
    for( ; (scanf("%d %d",&P,&T)==2) ; ID.clear() ) {
        if(P==0 && T==0)break;
        read();
        proc();
    }

    return 0;
}

